package tv.huan.web.controller.media;

import java.util.List;

import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.media.domain.Album;
import tv.huan.media.service.IAlbumService;

/**
 * 专辑Controller
 * 
 * @author admin
 * @date 2020-04-03
 */
@Controller
@RequestMapping("/media/album")
public class AlbumController extends BaseController
{
    private String prefix = "media";

    @Autowired
    private IAlbumService albumService;

    @RequiresPermissions("media:album:view")
    @GetMapping()
    public String album()
    {
        return prefix + "/album";
    }

    /**
     * 查询专辑列表
     */
    @RequiresPermissions("media:album:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Album album)
    {
        startPage();
        List<Album> list = albumService.selectAlbumList(album);
        return getDataTable(list);
    }

    /**
     * 导出专辑列表
     */
    @RequiresPermissions("media:album:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Album album)
    {
        List<Album> list = albumService.selectAlbumList(album);
        ExcelUtil<Album> util = new ExcelUtil<Album>(Album.class);
        return util.exportExcel(list, "album");
    }

    /**
     * 新增专辑
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专辑
     */
    @RequiresPermissions("media:album:add")
    @Log(title = "专辑", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Album album)
    {
        return toAjax(albumService.insertAlbum(album));
    }

    /**
     * 修改专辑
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Album album = albumService.selectAlbumById(id);
        mmap.put("album", album);
        return prefix + "/edit";
    }

    /**
     * 修改保存专辑
     */
    @RequiresPermissions("media:album:edit")
    @Log(title = "专辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Album album)
    {
        return toAjax(albumService.updateAlbum(album));
    }

    /**
     * 删除专辑
     */
    @RequiresPermissions("media:album:remove")
    @Log(title = "专辑", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(albumService.deleteAlbumByIds(ids));
    }

    /**
     * 查询审核小组列表 用来将专辑分配
     */
    @RequiresPermissions("media:album:assignTeam")
    @GetMapping("/assignTeam/{albumIds}")
    public String assignTeam(@PathVariable("albumIds") String albumIds, ModelMap mmap)
    {
        mmap.put("albumIds", albumIds);
        return prefix + "/assignTeam";
    }

    /**
     * 专辑分配（保存）
     */
    @RequiresPermissions("media:album:assignTeam")
    @PostMapping("/assignTeam")
    @ResponseBody
    public AjaxResult assignTeam(String albumIds, Integer teamId)
    {
        try {
            this.albumService.assignTeam(albumIds, teamId);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("分配成功");
    }
}
